package org.jeecg.modules.device.com.rabbitmq;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.device.com.pudong.entity.DeviceCmdLog;
import org.jeecg.modules.device.com.pudong.service.IDeviceCmdLogService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.Map;


@Component
@AllArgsConstructor
@Slf4j
public class DeviceCmdConsumer {

    @Autowired
    IDeviceCmdLogService deviceCmdLogService;

    @RabbitListener(queues = "deviceCmdQueue")
    public void process(Map<String,Object> map) {

            log.info("收到设备指令消息: {}", map);

            if (map.isEmpty()) {
                log.warn("收到空指令消息");
                return;
            }

            String cmdType = "未知指令";
            String cmdResult = "失败";

            // 使用entrySet遍历
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();

                try {
                    if ("set_reportedTime".equals(key)) {
                        cmdType = "修改上报频率";
                        JSONObject json = JSONUtil.parseObj(value);
                        String result = json.getStr("result", "F").toUpperCase(); // 默认值F
                        cmdResult = "T".equals(result) ? "成功" : "失败";
                        log.info("处理{}命令，结果：{}", cmdType, cmdResult);
                    }
                    else if ("deviceDataSwitch".equals(key)) {
                        cmdType = "数据传输开关";
                        JSONObject json = JSONUtil.parseObj(value);
                        String result = json.getStr("result", "F").toUpperCase();
                        cmdResult = "T".equals(result) ? "成功" : "失败";
                        log.info("处理{}命令，结果：{}", cmdType, cmdResult);
                    }
                    else if ("set_datetime".equals(key)) {
                        cmdType = "设置设备时间";
                        JSONObject json = JSONUtil.parseObj(value);
                        String result = json.getStr("result", "F").toUpperCase();
                        cmdResult = "T".equals(result) ? "成功" : "失败";
                        log.info("处理{}命令，结果：{}", cmdType, cmdResult);
                    }
                    else if ("gettime".equals(key)) {
                        cmdType = "校正设备时间";
                        // 保留原有空逻辑，但添加日志
                        log.warn("该命令暂无处理逻辑: {}", key);
                    }
                    else {
                        log.warn("收到未知指令类型: {}", key);
                    }
                } catch (Exception e) {
                    log.error("处理{}命令时发生异常: {}", key, e.getMessage());
                    cmdResult = "解析异常";
                }
            }

            // 后续处理（可保留原有逻辑）
            log.debug("最终指令类型: {}, 处理结果: {}", cmdType, cmdResult);
        DeviceCmdLog deviceCmdLog = new DeviceCmdLog();
        deviceCmdLog.setCmdType(cmdType);
        deviceCmdLog.setCmdType(cmdResult);
        deviceCmdLogService.save(deviceCmdLog);

    }
}